<?php
/**
 * 广告管理
 * @author Ice iceinto@mallog.com.cn
 */
class AdModel extends BaseModel
{
    //自动验证
    protected $_validate = array(
        array("name", "require", "名称必须！", 1),
        array('type', 'require', "类型必须！", 1)
    );

    /**
     * 获取广告
     * @author Ice iceinto@mallog.com.cn
     */
    public function getAllPro($where = null)
    {
        $count = 0;
        if ($where) {
            $count = M('AdAllot')->where($where)->count();
        } else {
            $count = $this->where($where)->count();
        }
        $Page = $this->getPage($count);
        $AdAllotModel = D('AdAllot');
        $map = array();
        $data = $this->field(
            'm_ad.id,
            m_ad.name as m_ad_name,
            m_ad.id,
            m_ad.image,
            m_ad_type.name as m_ad_type_name')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('m_ad.id desc')
            ->join('LEFT JOIN m_ad_type ON m_ad.type = m_ad_type.id')
            ->join('LEFT JOIN m_ad_allot ON m_ad.id = m_ad_allot.id')
            ->order('id DESC')
            ->where($where)
            ->select();
        foreach ($data as $k => $ad) {
            $map['id'] = $ad['id'];
            $adAllot = $AdAllotModel->where($map)->find();
            if ($adAllot) {
                $data[$k]['adAllot'] = $this->getAdShow($adAllot['id']);
            }
        }
        return array('page' => $Page->show(), 'data' => $data);
    }

    public function getAdShow($aId)
    {
        $data = array();
        //sqlSELECT
        $AdAllotModel = D('AdAllot');
        $data = $AdAllotModel->field(
            'm_ad_allot.id,
                m_ad_allot.level,
                m_ad_allot.mid,
                m_ad_allot.fid,
                m_ad_allot.bid,
                m_market.name as m_name,
                m_floor.name as f_name,
                m_brand.name as b_name,
                m_floor.sain as f_sain')
            ->join('LEFT JOIN m_market ON m_ad_allot.mid = m_market.id')
            ->join('LEFT JOIN m_floor ON m_ad_allot.fid = m_floor.id')
            ->join('LEFT JOIN m_brand ON m_ad_allot.bid = m_brand.id')
            ->where(array('m_ad_allot.id' => $aId))
            ->find();
        return $data;
    }

    /**
     * 获取没有设置广告位的广告信息
     * @return mixed
     */
    public function getNoShow()
    {
        $data = $this->query('SELECT * FROM m_ad WHERE m_ad.id NOT IN (SELECT id FROM m_ad_allot ) ORDER BY m_ad.id DESC  LIMIT 0,20');
        return $data;
    }

    private $_type = 'ad';

    /**
     * 返回需要的公告图片信息
     * @param Array $map 获取数据条件
     * @param String $field 字段
     * @param String $order 排序
     * @param String $limit 分页
     * @return Array 返回广告数组
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getAd($map, $field = 'm_ad.*,m_ad_allot.*', $order = 'm_ad_allot.ordery DESC', $limit = '')
    {
        $data = array();
        $data = $this->where($map)
            ->field($field)
            ->join('INNER JOIN m_ad_allot ON m_ad.id = m_ad_allot.a_id')
            ->join('INNER JOIN m_ad_type ON m_ad.type = m_ad_type.id')
            ->order($order)
            ->limit($limit)
            ->select();
        //SD($this->getLastSql());
        //SD($data);
        foreach ($data as $k => $v) {
            $data[$k]['imageUrl'] = $this->getManagedUrl($v['image'], $this->_type);
            //只有首页和商场滚动广告图，其他广告全是品牌广告
            $url = '';
            if ($v['m_id'] && (!$v['f_id'] || !$v['b_id'])) {
                $url = $this->getMallUrl($v['m_id']);
            } else if ($v['m_id'] && $v['f_id'] && $v['b_id']) {
                $url = $this->getAdUrl($v);
            } else {
                $url = "javascript:;";
            }
            $data[$k]['adUrl'] = $url;
        }
        return $data;
    }

    /**
     * 返回广告连接地址
     * @param Array $v
     * @return String
     */
    public function getAdUrl($v)
    {
        $map = array();
        $url = '';
        $map['b_m_id'] = $v['m_id'];
        $map['b_f_id'] = $v['f_id'];
        $map['b_b_id'] = $v['b_id'];
        $floorBrand = M('FloorBrand')->where($map)->find();
        //获取楼层信息
        unset($map);
        $map['f_id'] = $v['f_id'];
        $floor = M('Floor')->field('f_id,f_m_id,f_sain,f_name')->where($map)->find();
        //获取品牌信息
        unset($map);
        $map['b_id'] = $v['b_id'];
        $brand = M('Brand')->field('b_id,b_name,b_e_name')->where($map)->find();
        //获取商场信息
        unset($map);
        $map['m_id'] = $v['m_id'];
        $mall = M('Market')->field('m_id,m_initial,m_name')->where($map)->find();
        if ($floorBrand && $floor && $brand && $mall) {
            $url = U($mall['m_initial'] . '/' . strtolower($floor['f_sain']) . '/' . $brand['b_e_name']);
        }
        return $url;
    }

    public function getMallUrl($mId)
    {
        $map = array();
        $url = '';
        $map['m_id'] = $mId;
        $mall = M('Market')->field('m_id,m_initial,m_name')->where($map)->find();
        if ($mall) {
            $url = U($mall['m_initial'] . '/index');
        }
        return $url;
    }
}

?>